namespace PickGold.Charting
{
    using System;
    using System.Collections;
    using System.ComponentModel;

    internal class LegendAreaNameConverter : StringConverter
    {
        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            ArrayList values = new ArrayList();
            values.Add("NotSet");
            ChartAreaCollection chartAreas = null;
            string name = "";
            if ((context != null) && (context.Instance != null))
            {
                if (context.Instance is Legend)
                {
                    Legend instance = (Legend) context.Instance;
                    if ((instance.Common != null) && (instance.Common.ChartPicture != null))
                    {
                        chartAreas = instance.Common.ChartPicture.ChartAreas;
                    }
                }
                else if (context.Instance is ChartArea)
                {
                    ChartArea area = (ChartArea) context.Instance;
                    if ((area.Common != null) && (area.Common.ChartPicture != null))
                    {
                        chartAreas = area.Common.ChartPicture.ChartAreas;
                        name = area.Name;
                    }
                }
                else if (context.Instance is Title)
                {
                    Title title = (Title) context.Instance;
                    if ((title.Chart != null) && (title.Chart.ChartPicture != null))
                    {
                        chartAreas = title.Chart.ChartPicture.ChartAreas;
                    }
                }
                else if (context.Instance is Annotation)
                {
                    Annotation annotation = (Annotation) context.Instance;
                    if ((annotation.Chart != null) && (annotation.Chart.ChartPicture != null))
                    {
                        chartAreas = annotation.Chart.ChartPicture.ChartAreas;
                    }
                }
                else if (context.Instance is IServiceProvider)
                {
                    IServiceProvider provider = context.Instance as IServiceProvider;
                    Chart service = provider.GetService(typeof(Chart)) as Chart;
                    if (service != null)
                    {
                        chartAreas = service.ChartAreas;
                    }
                }
                else if (context.Instance is Array)
                {
                    if ((((Array) context.Instance).Length > 0) && (((Array) context.Instance).GetValue(0) is Legend))
                    {
                        Legend legend2 = (Legend) ((Array) context.Instance).GetValue(0);
                        if ((legend2.Common != null) && (legend2.Common.ChartPicture != null))
                        {
                            chartAreas = legend2.Common.ChartPicture.ChartAreas;
                        }
                    }
                    else if ((((Array) context.Instance).Length > 0) && (((Array) context.Instance).GetValue(0) is ChartArea))
                    {
                        ChartArea area2 = (ChartArea) ((Array) context.Instance).GetValue(0);
                        if ((area2.Common != null) && (area2.Common.ChartPicture != null))
                        {
                            chartAreas = area2.Common.ChartPicture.ChartAreas;
                        }
                    }
                    else if ((((Array) context.Instance).Length > 0) && (((Array) context.Instance).GetValue(0) is Title))
                    {
                        Title title2 = (Title) ((Array) context.Instance).GetValue(0);
                        if ((title2.Chart != null) && (title2.Chart.ChartPicture != null))
                        {
                            chartAreas = title2.Chart.ChartPicture.ChartAreas;
                        }
                    }
                    else if ((((Array) context.Instance).Length > 0) && (((Array) context.Instance).GetValue(0) is Annotation))
                    {
                        Annotation annotation2 = (Annotation) ((Array) context.Instance).GetValue(0);
                        if ((annotation2.Chart != null) && (annotation2.Chart.ChartPicture != null))
                        {
                            chartAreas = annotation2.Chart.ChartPicture.ChartAreas;
                        }
                    }
                }
            }
            if (chartAreas != null)
            {
                foreach (ChartArea area3 in chartAreas)
                {
                    if (area3.Name != name)
                    {
                        values.Add(area3.Name);
                    }
                }
            }
            return new TypeConverter.StandardValuesCollection(values);
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return false;
        }

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
    }
}

